package com.webserver.http;



import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class HttpResponse {
    //状态行相关信息
    private String protocol = "HTTP/1.1";  //协议版本
    //默认200，OK的原因是大部分响应都是正常的
    private int statusCode = 200;  //状态代码
    private String statusReason = "OK"; //状态描述
    //响应头相关信息

    //相应正文相关信息
    private File entity;  //正文对应的一个实际文件

    private  Socket socket;


    //构造方法（一定要记住 传入out用）
    public HttpResponse(Socket socket){
        this.socket = socket;
    }

    /**
     * 将当前响应对象内容按照标准的HTTP响应格式发送客户端
     */
    public void flush() throws IOException {
        /*HTTP/1.1 200 OK(CRLF)
            Content-Type: text/html(CRLF)
            Content-Length: 2546(CRLF)(CRLF)
                    1011101010101010101......*/
                /*String line ="HTTP/1.1 200 OK";
                byte[] data = line.getBytes("ISO8859-1");
                out.write(data);
                out.write(13);
                out.write(10);

                data = "Content-Type: text/html(CRLF)";
           */
        //3 放送响应

        //3.1：发送状态行
        sendStatusLine();


        //3.2:发送响应头
        sendHeaders();

        //3.3:发送正文（index页面内容）
        sendContent();
    }
    private void sendStatusLine() throws IOException {
        //UnsupportedEncodingException 原因：字符集名字拼写错误
        String line = protocol+" "+statusCode+" "+statusReason;
        println(line);
    }
    private void sendHeaders() throws IOException {
        String line = "Content-Type: text/html";
        println(line);


        line = "Content-Length: "+entity.length();
        println(line);

        //单独发送CRLF表示响应头发送完毕
        println("");
    }
    private void sendContent() throws IOException {
        OutputStream out = socket.getOutputStream();  //解决out异常
        try (
                FileInputStream fis = new FileInputStream(entity);  //读取文件
        )
        {


            byte[] buf = new byte[1024*8];  //建立字符数组用来循环保存字符
            int len; //整数来看文件是否读完
            while((len =fis.read(buf))!=-1){  //先处理read异常
                out.write(buf,0,len);
            }
        }
    }

    private void println(String line) throws IOException {
        OutputStream out  =socket.getOutputStream();
        byte[] data = line.getBytes("ISO8859-1");
        out.write(data);
        out.write(13);  //written CR
        out.write(10);  //written LF
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusReason() {
        return statusReason;
    }

    public void setStatusReason(String statusReason) {
        this.statusReason = statusReason;
    }

    public File getEntity() {
        return entity;
    }

    public void setEntity(File entity) {
        this.entity = entity;
    }


}